package com.android.hidephotosplus;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.android.hidephotosplus.adapter.AlbumAdapter;
import com.android.hidephotosplus.db.DbProvider;
import com.android.hidephotosplus.model.ModelAlbum;
import com.android.hidephotosplus.model.ModelMedia;
import com.android.hidephotosplus.util.Config;
import com.android.hidephotosplus.util.PasswordManager;

public class ActivityGalleryForResult extends SherlockActivity implements
		OnScrollListener, OnItemClickListener {

	public GridView mGridView;

	private AlbumAdapter mAlbumAdapter;

	private ArrayList<ModelAlbum> arrAlbums = new ArrayList<ModelAlbum>();

	private DbProvider mDbProvider;

	private ActionBar mActionBar;

	private String nameAlbumHidden = null;

	private long idAlbumHidden = -1;
	
	public static boolean IS_START_PASSWORD = false;

	public PasswordManager mPasswordManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery_fragment);
		mActionBar = getSupportActionBar();
		mActionBar.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.bg_actionbar));
		
		mPasswordManager = new PasswordManager(this);

		mActionBar.setTitle(getString(R.string.gallery_stock));

		mDbProvider = DbProvider.getinstance(getApplicationContext());
		mGridView = (GridView) findViewById(R.id.gridAlbum);
		mGridView.setOnScrollListener(this);
		mGridView.setOnItemClickListener(this);

		arrAlbums = mDbProvider.getAllAlbum();
		mAlbumAdapter = new AlbumAdapter(getApplicationContext(), arrAlbums,
				true);
		mGridView.setAdapter(mAlbumAdapter);

		Intent intent = getIntent();
		if (intent.getExtras() != null) {
			nameAlbumHidden = intent.getExtras().getString(
					Config.KEY_NAME_ALBUM_HIDDEN);
			idAlbumHidden = intent.getExtras().getLong(
					Config.KEY_ID_ALBUM_HIDDEN);
		}
	}
	
	@Override
	public void onPause() {
		super.onPause();
		IS_START_PASSWORD = true;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (IS_START_PASSWORD) {
			mPasswordManager.checkSetupOrUnlockPass();
		}
	}

	@Override
	public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if (view.getId() == R.id.gridAlbum) {
			if (scrollState == SCROLL_STATE_FLING) {
				mAlbumAdapter.setScroolState(true);
			} else {
				mAlbumAdapter.setScroolState(false);
				mAlbumAdapter.notifyDataSetChanged();
			}
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		Intent intent = new Intent(this, ActivityAlbumViewForResult.class);
		intent.putExtra(Config.KEY_REQUEST_ARR_MEDIA_TO_HIDE,
				mAlbumAdapter.getItemId(position));
		intent.putExtra(Config.KEY_ALBUM_STOCK, mAlbumAdapter.getItem(position));
		intent.putExtra(Config.KEY_ID_ALBUM_HIDDEN, idAlbumHidden);
		intent.putExtra(Config.KEY_NAME_ALBUM_HIDDEN, nameAlbumHidden);
		startActivityForResult(intent,
				Config.REQUEST_CODE_START_ACTIVITY_GALLERY);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case Config.REQUEST_CODE_START_ACTIVITY_GALLERY:
			if (resultCode == RESULT_OK) {
				if (data.getExtras() != null) {
					setResult(RESULT_OK, data);
				}
			}
			finish();
			break;
		case Config.REQUEST_CODE_START_PATTERN_PASSWORD:
			if (resultCode == Activity.RESULT_OK) {
				IS_START_PASSWORD = false;
			}
			break;
		default:
			break;
		}

	}

}
